<template>
  <div>
    <h1>我的维修记录</h1>
    
    <!-- 搜索区域 -->
    <div class="search-bar">
      <el-input 
        v-model="searchParams.equipmentId" 
        placeholder="设备ID" 
        clearable 
        style="width: 200px; margin-right: 10px;"
      />
      <el-input 
        v-model="searchParams.anomalyId" 
        placeholder="异常ID" 
        clearable 
        style="width: 200px; margin-right: 10px;"
      />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </div>
    
    <!-- 维修记录表格 -->
    <el-table :data="repairs" style="width: 100%" border stripe>
      <el-table-column prop="repairId" label="维修ID" width="120" />
      <el-table-column prop="anomalyId" label="异常ID" width="140" />
      <el-table-column prop="equipmentId" label="设备ID" width="140" />
      <el-table-column prop="processorId" label="处理人ID" width="120" />
      
      <el-table-column prop="repairResult" label="维修结果" width="120">
        <template #default="{ row }">
          <el-tag :type="row.repairResult === 1 ? 'success' : 'warning'">
            {{ row.repairResult === 1 ? '已完成' : '未完成' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column prop="repairImages" label="维修图片" width="120">
        <template #default="{ row }">
          <el-image 
            v-if="row.repairImages" 
            :src="row.repairImages" 
            :preview-src-list="[row.repairImages]" 
            style="width: 50px; height: 50px" 
          />
        </template>
      </el-table-column>
      
      <el-table-column prop="repairTime" label="维修时间" width="180" />
      <el-table-column prop="repairNotes" label="维修备注" />
      
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="openEditDialog(row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px; text-align: right;"
    />
    
    <!-- 编辑对话框 -->
    <el-dialog title="编辑维修记录" v-model="editDialogVisible">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="维修结果">
          <el-select v-model="editForm.repairResult" placeholder="请选择">
            <el-option label="未完成" :value="0" />
            <el-option label="已完成" :value="1" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="维修备注">
          <el-input 
            v-model="editForm.repairNotes" 
            type="textarea" 
            :rows="3" 
          />
        </el-form-item>
        
        <el-form-item label="维修图片">
          <el-upload
            action="/api/upload" 
            :on-success="handleUploadSuccess"
            :show-file-list="false"
            :before-upload="beforeUpload"
          >
            <el-button type="primary">上传图片</el-button>
            <template #tip>
              <div class="el-upload__tip">支持jpg/png格式，大小不超过2MB</div>
            </template>
          </el-upload>
          <el-image 
            v-if="editForm.repairImages" 
            :src="editForm.repairImages" 
            style="width: 100px; height: 100px; margin-top: 10px;" 
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { api } from "@/api";

export default {
  data() {
    return {
      repairs: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      searchParams: {
        equipmentId: '',
        anomalyId: ''
      },
      editDialogVisible: false,
      editForm: {
        repairId: null,
        repairResult: null,
        repairNotes: '',
        repairImages: ''
      }
    };
  },
  created() {
    this.fetchRepairs();
  },
  methods: {
    async fetchRepairs() {
      this.loading = true;
      try {
        // 获取当前登录用户ID
        const currentUserId = localStorage.getItem('userId');
        
        const params = {
          page: this.currentPage,
          pageSize: this.pageSize,
          processorId: currentUserId, // 仅查询当前用户的记录
          ...this.searchParams       // 包含搜索条件
        };
        
        const response = await api.repair.list(params);
        
        if (response.data.code === 200) {
          this.repairs = response.data.data.records;
          this.total = response.data.data.total;
        } else {
          this.$message.error(response.message || '获取数据失败');
        }
      } catch (error) {
        console.error('获取维修记录失败:', error);
        this.$message.error('网络错误，请稍后重试');
      } finally {
        this.loading = false;
      }
    },
    
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.fetchRepairs();
    },
    
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.fetchRepairs();
    },
    
    // 搜索功能
    handleSearch() {
      this.currentPage = 1;
      this.fetchRepairs();
    },
    
    // 重置搜索条件
    resetSearch() {
      this.searchParams = {
        equipmentId: '',
        anomalyId: ''
      };
      this.fetchRepairs();
    },
    
    // 打开编辑对话框
    openEditDialog(row) {
      this.editForm = {
        repairId: row.repairId,
        repairResult: row.repairResult,
        repairNotes: row.repairNotes,
        repairImages: row.repairImages
      };
      this.editDialogVisible = true;
    },
    
    // 图片上传处理
    handleUploadSuccess(response) {
      if (response.code === 200) {
        this.editForm.repairImages = response.data.url;
        this.$message.success('图片上传成功');
      } else {
        this.$message.error(response.message || '图片上传失败');
      }
    },
    
    // 图片上传前校验
    beforeUpload(file) {
      const isImage = file.type.includes('image');
      const isLt2M = file.size / 1024 / 1024 < 2;
      
      if (!isImage) {
        this.$message.error('只能上传图片格式!');
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过2MB!');
      }
      
      return isImage && isLt2M;
    },
    
    // 提交编辑
    async submitEdit() {
      try {
        const response = await api.repair.update(this.editForm);
        
        if (response.data.code === 200) {
          this.$message.success('维修记录更新成功');
          this.fetchRepairs();
          this.editDialogVisible = false;
        } else {
          this.$message.error(response.data.message || '更新失败');
        }
      } catch (error) {
        console.error('更新维修记录失败:', error);
        this.$message.error('网络错误，请稍后重试');
      }
    }
  }
};
</script>

<style scoped>
.search-bar {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.el-table {
  margin-top: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-table::v-deep .el-table__row:hover {
  background-color: #f5f7fa;
}
</style>